<?php

/***  DOCUMENTATION LAYER

Last Update: Jun 2007
	
DESCRIPTION
	In short, use this script as a cron job to mail a summary for several profiles
	to yourself at regular (daily) intervals.
	
	Using Greqo Analytics class, this scripts requests data for an array of 
	profiles displays a summary which can then be emailed.  Edit the dates and
	set up as a crontab for regular updates.
	
	To set up:
		1. Make sure Editable Settings below are correct.
		2. Upload Greqo to server
		3. Upload script to proper dir in server.
		4. Set crontab
	
	recommended cron settings:
		lynx -dump 'http://www.YOURDOMAIN.com/_cron_guarded/analytic.daily.php?cron=cron'
		
	others
		wget -O /dev/null 'http://www.YOURDOMAIN.com/_cron_guarded/analytic.daily.php?cron=cron'
		
	designed to allow testing in browser.  once it has been tested, move to the
	$safe_dir_name, where it will only be accessible by a local script.  not the
	supermax secure, but good enough in most cases
	
***/

// Editable Settings
$_DIR['greqo'] = ( $_SERVER['HTTP_HOST'] == 'localhost' ) ? $_SERVER['DOCUMENT_ROOT'] . "/GREQO/" : dirname($_SERVER['DOCUMENT_ROOT']) . "/GREQO/";
$safe_dir_name = '_cron_guarded';
$debug = 0;
$acct_email = 'USER@gmail.com';
$acct_pw = 'PASSWORD';
$mail_to = 'USER@gmail.com';
$send_email = 0;

// Profile IDs -- ids for profiles being reported
$PROFILE = array('0000001','0000002','0000003');

// Kill Switch
if ( 0 ) die('<h2>this script has been disabled</h2><a href="http://code.google.com/p/greqo/">Project Greqo</a>');

// Cron Guard
if ( strpos($_SERVER['PHP_SELF'], $safe_dir_name) && $_SERVER['REMOTE_ADDR'] !== $_SERVER['SERVER_ADDR'] )
	die('<a href="http://code.google.com/p/greq">Project Greqo</a><!-- local cron access only -->');

// Load Files
require_once($_DIR['greqo'] . 'analytics.class.php');
require_once($_DIR['greqo'] . 'gmail.class.php');

// Load Greqo Object
$GreqoAnalytics = new GreqoAnalytics($debug);
$GreqoAnalytics->print_d('cron testing : don\'t forget to GET the cron');

// Get-Activated Portion of Script
if ( $_GET['cron'] == 'cron' )
{
	// access settings
	$GreqoAnalytics->email = $acct_email;
	$GreqoAnalytics->password = $acct_pw;
	
	// clean up cache
	$GreqoAnalytics->clean_cache();
	
	// get reports
	$summary = '';
	$start_uts = time() - $GreqoAnalytics->day_sec;
	$stop_uts = $start_uts;
	foreach ( $PROFILE as $pid )
	{
		$summary .= $GreqoAnalytics->get_site_summary($start_uts, $stop_uts, $pid);
		$summary .= "\n\n============================\n\n";
	}
	echo "<pre>\n$summary\n</pre>";


	// mail?
	if ( !$send_email )
	{
		die('no mail');
	}
	
	// prepare gmailer
	$Mailer = new GmailMailer($debug=0);
	$Mailer->Username = $acct_email;
	$Mailer->Password = $acct_pw;
	$Mailer->FromName = 'Greqo Mailer';
	$Mailer->AddAddress($mail_to, 'Analytics User');
	$Mailer->Subject = 'Daily Greqo Analytics Report';
	$Mailer->Body = $summary;
	
	// send email
	if ( !$Mailer->send_email() ) 
	{
		$Mailer->print_d('sorry, unable to deliver email', 'red');
	}
	else
	{
		$Mailer->print_d("mail sent to $mail_to", 'green');
	}
}

?>
